// dao/userDao.js
// ʵ����MySQL����
var mysql = require('mysql');
var $conf = require('../../conf/db');
var $util = require('../utils/util');
var $sql = require('../mapping/activeSqlMapping');

var pool = mysql.createPool($util.extend({}, $conf.mysql));


module.exports = {

    queryAll: function (req,param,pageNo, callback) {

        $util.queryHelper(req, $sql.queryAll, ['%'+param+'%',5 * pageNo], pool, function (result,err) {
            callback(result,param);
        })
    },
    insert: function (req, detailId, callback) {

        $util.queryHelper(req, $sql.insert, [req.param('subject'), req.param('startTime'), req.param('number'), detailId, req.param('location')], pool, function (result) {
            callback(result);
        })
    },

    queryById: function (req, callback) {
        $util.queryHelper(req, $sql.queryById, [req.param('id')], pool, function (result) {
            callback(result);
        })
    },

    queryActiveByName: function (req, callback) {  //活动搜索（活动主题）
        $util.queryHelper(req, $sql.queryByName, ['%' + req.body.name + '%'], pool, function (result) {
            callback(result);
        })
    },

    insertInvite: function (req, res, callback) {
        var param = req.query || req.params;
        $util.queryHelper(req, $sql.insertInvite, [req.param('activeId')], pool, function (result) {
            callback(result);
        })
    },


    updateActive: function (req, callback) {
        $util.queryHelper(req, $sql.updateActive, [req.body.subject, req.body.startTime, req.body.number, req.body.location, req.body.id], pool, function (result) {
            callback(req);
        })
    },


    delActivity: function (req, res, callback) { //管理员删除活动

        //console.log(userInfoId);
        // var date = this.getDate();
        pool.getConnection(function (err, connection) {
            connection.query($sql.del, [req], function (err, result) {
                if (result.affectedRows >= 1) {
                    result = {
                        code: 200,
                        content: req,
                    };
                    callback(result);

                } else {
                    callback("no");
                }
                // �ͷ�����
                connection.release();
            });
        });
    },
    queryNews: function (req, callback) {  //活动搜索（活动主题）
    $util.queryHelper(req, $sql.queryNews, [],pool, function (result) {
        callback(result);
    })
},

};